package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.SendDonTaiAdapter;
import test5.lupeng.jf.com.teamdemo.bean.RequestDonTai;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

import static android.content.ContentValues.TAG;

//发布动态
public class SendDonTaiActivity extends AppCompatActivity {

    private final static int SELECT_ORIGINAL_PIC=126;
    private static final String TAG = "SendDonTaiActivity";
    private EditText ed_text;

    private RecyclerView newImageVp;
    private int i = 1;
    private String userId;
    private String message;
    ArrayList<String> strings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_don_tai);
        ed_text = (EditText) findViewById(R.id.ed_text);
        newImageVp = (RecyclerView) findViewById(R.id.NewImageVp);
        strings = new ArrayList<>();

        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        userId = sp.getString("objectId","0");


    }

    //发布动态
    public void Send(View view) {
        message = ed_text.getText().toString();
        if (message.equals("")) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
        }else{
            //网络请求发布动态


            RequestDate(strings);
        }



    }

    public void RequestDate(ArrayList<String> list) {

        String url = "https://leancloud.cn:443/1.1/functions/shareStatu";
        RequestDonTai object = new RequestDonTai(userId, list, message);
        System.out.println(list.toString());
        Request request = HTTPUtils.requestPost(object, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: " + result);
                if (response.code() == 200) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(SendDonTaiActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(true);
                            finish();
                        }
                    });

                }
            }
        });
    }

    //点击添加图片
    public void SendImage(View view){
        getImageFromAlbum();
    }

    public void getImageFromAlbum(){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");  //相片类型
        startActivityForResult(intent,SELECT_ORIGINAL_PIC);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case SELECT_ORIGINAL_PIC:
                if (resultCode == RESULT_OK) {
//                    ContentResolver resolver = getContentResolver();
//                    Bitmap bitmap = null;
//                    Uri uri = data.getData();
//
//                    //获取图片的路径
//                    uri = data.getData();  //获取系统返回的照片的Uri
//                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
//                    //从系统表中查询指定Uri对应的照片
//                    Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
//                    cursor.moveToFirst();
//                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//                    //获取照片路径
//                    String picturePath = cursor.getString(columnIndex);
//                    cursor.close();
//                    Log.i(TAG, "onActivityResult: " + picturePath);
//                    //存入集合

                    Uri uri = data.getData();
                    Log.i(TAG, "onActivityResult: " + uri);
                    // 将uri 类型的地址, 转化成String 类型的地址  就是绝对路径  用于上传
                    String[] proj = {MediaStore.Images.Media.DATA};
                    Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
                    int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    actualimagecursor.moveToFirst();
                    String img_path = actualimagecursor.getString(actual_image_column_index);


                    strings.add(img_path);
                    initView();
                }

                break;
        }
    }

    //在这里面设置图片
    public void initView(){
        //使其可见
        newImageVp.setVisibility(View.VISIBLE);

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        newImageVp.setLayoutManager(layoutManager);
        SendDonTaiAdapter sendDonTaiAdapter = new SendDonTaiAdapter(strings);
        newImageVp.setAdapter(sendDonTaiAdapter);
        sendDonTaiAdapter.setListen(new SendDonTaiAdapter.SendDonTaiInterface() {
            @Override
            public void clickListen(int position) {
                strings.remove(position);
                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                newImageVp.setLayoutManager(layoutManager);
                SendDonTaiAdapter NewsendDonTaiAdapter = new SendDonTaiAdapter(strings);
                newImageVp.setAdapter(NewsendDonTaiAdapter);
                RequestDate(strings);
            }

            //将View

        });






    }




    //返回按钮
    public void return5(View view){
        finish();
    }
}
